// miniprogram/pages/detailPosition.js
let network = require('../../../utils/requestManager.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ID: '',
    vocation: '',
    shop_title: '',
    shopImage: '',
    count: '',
    age: '',
    money: '',
    shop_phone:'',
    obligation: '',
    duty: '',
    bind_phone: '',
    welfareList: '',
    shop_address: '',
    showPhoneModalStatus: false,
    productImage:'',
    qrcode:'',
    //没有手机号true是有手机号码
    click:true,
    user_phone:'',
    canvasShow: false,
    sign_disable:false,
    user_sign_status:"立即应聘",
    shop_id:"",
    shop_banner:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that=this
    if (options.shop) {
      getApp().globalData.shop = options.shop
      this.setData({
        ID: options.ID
      })
    } else {
      this.setData({
        ID: options.ID
      })
    }
    //生成二维码时传入的scene
    let scene = decodeURIComponent(options.scene);
    //页面进入
    if (scene == 'undefined') {
      ///转发进来更新shop的id
      if (options.shop) {
        getApp().globalData.shop = options.shop
        this.setData({
          ID: options.ID
        })
      } else {
        this.setData({
          ID: options.ID
        })
      }
      detailPages(this)
    }
    //扫码进入
    else {
      network.getSceneFromMiniCodeBySceneID(scene, function (res) {
        console.log('------------------scene---------------------')
        console.log(res)
        getApp().globalData.shop = res.detail.shop
        that.setData({
          ID: res.detail.id
        })
        detailPages(that)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      
    }
    let that = this
    let shop_id = that.data.shop_id
    console.log(shop_id)
    return {
      title: "招贤纳士",
      imageUrl: that.data.shop_banner,
      path: '/pages/showView/recruit/detailPosition?shop=' + shop_id + '&ID=' + that.data.ID,
      success: function (res) {
        that.setData({
          shareFriendsMask: false
        })
        // 转发成功
        shareCard(this)
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  applyPosition: function() {
    let that=this
    //报名前判断手机号是否绑定
    let click = this.data.click
    if (!click) {
      this.setData({
        showPhoneModalStatus: true
      })
    } else {
      let params = network.shopParams()
      let url = 'recruitment/contact/info/create?id=' + this.data.ID
      console.log(url)
      network.postRequest(url, params, function (res) {
        console.log('---------------------------create-------------')
        console.log(res)
        let status = res.status
        if (status == 0) {//提交之后修改报名状态
          that.setData({
            sign_disable: true,
            user_sign_status: "已报名"
          })
          wx.showModal({
            title: '提示',
            content: '您的应聘信息已提交，请耐心等候门店负责人联系您哦！',
            showCancel: false,
            confirmColor: "#F09D3E"
          })
        }

      })

    }
  },
  phoneCall: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.shop_phone //仅为示例，并非真实的电话号码
    })
  },
  sharePicture: function (e) {
    /**
     *获取二维码
     * **/
   
    let params = {}
    let that = this
    network.ownerShare(function (shop_id) {
      console.log(shop_id)
      params['shop'] = shop_id
      params['id'] = that.data.ID
      params['width'] = 100
      params['page'] = 'pages/showView/recruit/detailPosition',
      params['type'] = 2

      let scene = {}
      scene['shop'] = shop_id
      scene['ID'] = that.data.ID
      params['scend'] = scene
      network.postRequest('xcx/create/qrcode', params, function (res) {
        console.log('-----------------------scene-------------------')
        console.log(res)
        // 生成二维码图片
        wx.getImageInfo({
          src: res.detail.path,
          success: function (image_url) {
            that.setData({
              qrcode: image_url.path
            })

            let canvas_data = {}
            canvas_data['title'] = that.data.shop_title
            canvas_data['position'] = that.data.vocation
            canvas_data['salary'] = that.data.money
            canvas_data['top_bg'] = that.data.productImage
            canvas_data['qrcode'] = that.data.qrcode
            let canvas_String = JSON.stringify(canvas_data)
            wx.navigateTo({
              url: '../../../pages/showView/recruit/shareRecruit?canvasData=' + canvas_String,
            })
          }
        })
      })
    })
  },
/**
 * 绑定手机号输入框
 * **/
bind_phone_input: function(e) {
  let that = this;
  that.setData({
    bind_phone: e.detail.value
  })
},
/**
 * 提交用户绑定手机号
 * **/
bind_user_phone: function(e) {
  let that = this;
  let reg = /1[\d]{10}/;
  let phone = this.data.bind_phone;
  if (!phone) {
    wx.showToast({
      title: '请填写手机号！',
      icon: 'none'
    })
    return;
  }
  if (!reg.test(phone)) {
    wx.showToast({
      title: '请填写正确的手机号！',
      icon: 'none'
    })
    return;
  }
  /**
   * 提交手机号
   * **/
  network.postRequest('xcx/bd/phone', {
    phone: phone
  }, function(res) {
    console.log(res)
    that.setData({
      showPhoneModalStatus: false,
      click:true
    })
    wx.showToast({
      title: '号码提交成功！',
      icon: 'none'
    })
  })
}
})

function detailPages(that) {
  console.log('token---------------------')
  console.log(getApp().globalData.token)
  let params = {}
  let url = 'recruitment/info/detail?id=' + that.data.ID
  console.log(url)
  network.getRequest(url, params, function(res) {
    console.log('------------------positionDetail--------------')
    console.log(res)
    let recruitment = res.detail.recruitment
    var welfare = recruitment.welfare
    let welfareList =JSON.parse(welfare)
    let sign_status = res.detail.apply
    if(sign_status){
      that.setData({
        user_sign_status: "已报名"
      })
    }
    that.setData({
      vocation: recruitment.vocation,
      shop_title: recruitment.shop_title,
      count: recruitment.number,
      age: recruitment.age,
      money: recruitment.money,
      shopImage: recruitment.image_url,
      obligation: recruitment.obligation,
      duty: recruitment.require,
      shop_address: recruitment.shop_address,
      welfareList: welfareList,
      shop_phone: recruitment.shop_phone,
      click:res.detail.click,
      sign_disable: sign_status,
      shop_id: recruitment.shopId,
      shop_banner: recruitment.shop_image_url

    })
    wx.getImageInfo({
      src: recruitment.image_url,
      success: function (image_url) {
        that.setData({
          productImage: image_url.path
        })
      }
    })
  })
}